﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GdHotel.Data;
using GdHotel.Data.Helpers;
using GdHotel.Data.Interfaces;
using GdHotel.Data.Resources;
using UniqueTechnologies.Attributes;
using UniqueTechnologies.Data.Interfaces;

namespace GdHotel.Web.Controllers
{
    [LanguageSupport]
    [Authorize(Roles = "Admin")]
    public class ManageNewsController : BaseController
    {
        private Size pictureSize = new Size(638, 353);
        private Size thumbnailSize = new Size(124, 91);

        public INewsRepository NewsRepository { get; set; }
        public override ILanguagesRepository LanguagesRepository { get; set; }

        public ManageNewsController(INewsRepository newsRepository, ILanguagesRepository languagesRepository)
        {
            NewsRepository = newsRepository;
            LanguagesRepository = languagesRepository;
        }

        //
        // GET: /ManageNews/

        public ViewResult Index()
        {
            var news = NewsRepository.FindAllByLang(Language).OrderByDescending(n => n.PublicationDate);
            return View(news);
        }

        //
        // GET: /ManageNews/Details/5

        public ViewResult Details(Int32 id)
        {
            var news = NewsRepository.GetById(id);
            return View(news);
        }

        //
        // GET: /ManageNews/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /ManageNews/Create

        [HttpPost]
        public ActionResult Create(News news, HttpPostedFileBase file)
        {
            if (ModelState.IsValid)
            {
                var picturePath = GraphicsHelper.SaveAndResizePicture(file, pictureSize, Server, Common.NewsPicturesDirectory);
                var thumbnailPath = GraphicsHelper.SaveAndResizePicture(file, thumbnailSize, Server, Common.NewsPicturesDirectory);
                news.PicturePath = picturePath ?? "";
                news.ThumbnailPath = thumbnailPath ?? "";
                news.LanguageId = Language.Id;
                NewsRepository.Insert(news);
                NewsRepository.Save();
                return RedirectToAction("Index");
            }

            return View(news);
        }

        //
        // GET: /ManageNews/Edit/5

        public ActionResult Edit(int id)
        {
            var news = NewsRepository.GetById(id);
            return View(news);
        }

        //
        // POST: /ManageNews/Edit/5

        [HttpPost]
        public ActionResult Edit(News news, HttpPostedFileBase file)
        {
            var dbNews = NewsRepository.GetById(news.Id);
            if (ModelState.IsValid)
            {
                var picturePath = dbNews.PicturePath;
                var thumbnailPath = dbNews.ThumbnailPath;
                try 
                {
                    if (file != null)
                    {
                        picturePath = GraphicsHelper.SaveAndResizePicture(file, pictureSize, Server, Common.NewsPicturesDirectory);
                        thumbnailPath = GraphicsHelper.SaveAndResizePicture(file, thumbnailSize, Server, Common.NewsPicturesDirectory);
                    }
                }
                catch
                {
                }

                dbNews.PicturePath = picturePath ?? "";
                dbNews.ThumbnailPath = thumbnailPath ?? "";
                UpdateModel(dbNews);
                NewsRepository.Save();
                return RedirectToAction("Index");
            }
            return View(news);
        }

        //
        // GET: /ManageNews/Delete/5

        public ActionResult Delete(int id)
        {
            var news = NewsRepository.GetById(id);
            return View(news);
        }

        //
        // POST: /ManageNews/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            var news = NewsRepository.GetById(id);
            NewsRepository.Delete(news);
            NewsRepository.Save();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            NewsRepository.Dispose();
            base.Dispose(disposing);
        }
    }
}